package cn.edu.glut.user.controller;

import javax.validation.Valid;

import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.glut.dto.Response;
import cn.edu.glut.dto.SecretaryInputDTO;
import cn.edu.glut.entity.Secretary;
import cn.edu.glut.user.service.SecretaryService;
/**
 * 
 * @author Caizhfy
 * @version v.0.1
 * @createTime 2017年12月13日下午8:42:08
 * @email Caizhfy@163.com
 * @description 秘书不提供上传功能了，人数较少，需要时再加接口
 *
 */
@RestController
@RequestMapping("/sec")
public class SecretaryController {
	@Autowired
	SecretaryService secretaryService;
	
	@RequestMapping("/new")
	public Response save(@Valid SecretaryInputDTO secretaryInputDTO){
		return Response.success(secretaryService.save(secretaryInputDTO.convertToEntity()));
	}
	
	@RequestMapping("/del/{id}")
	public Response delete(@PathVariable String id){
		secretaryService.delete(id);
		return Response.success();
	}
	
	@RequestMapping("/update")
	public Response update(@Valid SecretaryInputDTO secretaryInputDTO){
		Preconditions.checkNotNull(secretaryInputDTO.getId(), "id不能为空");
		secretaryService.update(secretaryInputDTO.convertToEntity());
		return Response.success();
	}
	
	@RequestMapping("/get/{id}")
	public Response get(@PathVariable String id){
		return Response.success(secretaryService.findById(id));
	}
	
	/**
	 * 
	 * @param pageable: size每页多少条（默认十条），page为第几页
	 * @param majorId	专业id号码（非必须）
	 * @param username	工号（非必须）
	 * @return
	 */
	@RequestMapping("/list")
	public Response list(@PageableDefault Pageable pageable, String majorId){
		return Response.success(secretaryService.list(pageable, majorId));
	}
	
	@RequestMapping("/get/um/{username}")
	public Response getByUsername(@PathVariable String username){
		return Response.success(secretaryService.findByUsername(username));
	}
	
	@RequestMapping("/dels")
	public Response delete(){
		secretaryService.deleteAll();
		return Response.success();
	}
	
}
